package com.cn.shuangzi.userinfo.activity;

import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.cn.shuangzi.SZApp;
import com.cn.shuangzi.SZBaseActivity;
import com.cn.shuangzi.activity.SZInterfaceActivity;
import com.cn.shuangzi.bean.VipInfo;
import com.cn.shuangzi.bean.VipPriceInfo;
import com.cn.shuangzi.common.SZConst;
import com.cn.shuangzi.loginplugin.common.socialized.pay.PayManager;
import com.cn.shuangzi.loginplugin.common.socialized.pay.PayType;
import com.cn.shuangzi.retrofit.SZRetrofitManager;
import com.cn.shuangzi.retrofit.SZRetrofitResponseListener;
import com.cn.shuangzi.userinfo.R;
import com.cn.shuangzi.util.SZTextViewWriterUtil;
import com.cn.shuangzi.util.SZToast;
import com.cn.shuangzi.util.SZValidatorUtil;
import com.cn.shuangzi.view.sweet_alert.SweetAlertDialog;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.umeng.commonsdk.statistics.common.DeviceConfig;


import java.util.List;

import io.reactivex.Observable;

/**
 * Created by CN.
 */

public abstract class SZPayActivity extends SZBaseActivity implements View.OnClickListener, SZInterfaceActivity {
    private TextView txtName;
    private TextView txtPrice;
    private TextView txtDesc;
    private Button btnPay;
    private ImageView imgCheckWechat;
    private ImageView imgCheckAlipay;
    private String payType = SZConst.ALIPAY;
    private VipPriceInfo vipPriceInfoCheck;
    private VipInfo vipInfoBought;
    @Override
    protected int onGetChildView() {
        return R.layout.activity_pay;
    }

    @Override
    protected void onBindChildViews() {

        txtName = findViewById(R.id.txtName);
        txtPrice = findViewById(R.id.txtPrice);
        txtDesc = findViewById(R.id.txtDesc);
        btnPay = findViewById(R.id.btnPay);
        imgCheckWechat = findViewById(R.id.imgCheckWechat);
        imgCheckAlipay = findViewById(R.id.imgCheckAlipay);

    }

    @Override
    protected void onBindChildListeners() {

    }

    @Override
    protected void onChildViewCreated() {
        onChildViewCreatedPre();
        showBackImgLeft(getBackImgLeft());
        setTitleTxt("支付订单");
        vipPriceInfoCheck = getSerializableExtra();
        if (vipPriceInfoCheck == null) {
            finish();
            return;
        }
        SZTextViewWriterUtil.writeValue(txtName, vipPriceInfoCheck.getFpSubject());
//        SZTextViewWriterUtil.writeValue(txtDesc, vipPriceInfoCheck.getFpOriginalPrice() + "元/月");
        SZTextViewWriterUtil.writeValue(txtPrice, vipPriceInfoCheck.getFpPrice());
    }

    @Override
    protected void onReloadData(boolean isRefresh) {

    }

    private void submitOrder() {
        btnPay.setEnabled(false);
        showBar();
        request(SZRetrofitManager.getInstance().getSZRequest().submitOrder(getUserId(), payType, DeviceConfig.getDeviceIdUmengMD5(this), vipPriceInfoCheck.getFpId()), new SZRetrofitResponseListener.SimpleSZRetrofitResponseListener() {
            @Override
            public void onSuccess(String data) {
                closeBar();
                new PayManager(getActivity(), SZConst.ALIPAY.equals(payType) ? PayType.ALIPAY : PayType.WECHAT, data, new PayManager.OnPayListener() {
                    @Override
                    public void onPaySuccess() {
                        try {
                            for(Activity activity : SZApp.getInstance().getStackActivity()){
                                if(activity instanceof SZBuyVipActivity){
                                    activity.finish();
                                    break;
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        onPayDoneSuccess(payType);
                        requestVipUserInfo(getVipUserObservable());
                        SweetAlertDialog sweetAlertDialog = showAlert(SweetAlertDialog.SUCCESS_TYPE, "支付成功", "恭喜您，已成功购买" + vipPriceInfoCheck.getFpSubject(), "确定", new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sweetAlertDialog) {
                                sweetAlertDialog.dismissWithAnimation();
                                finish();
                            }
                        }, null, null, false);
                        sweetAlertDialog.setCancelable(false);
                    }

                    @Override
                    public void onPayCancel() {
                        btnPay.setEnabled(true);
                        SZToast.warning("您取消了支付!");
                    }

                    @Override
                    public void onPayFailed() {
                        btnPay.setEnabled(true);
                        SZToast.error("支付失败,请重试!");
                    }
                }).toPay();
            }

            @Override
            public void onNetError(int errorCode, String errorMsg) {
                btnPay.setEnabled(true);
                closeBar();
                SZToast.error("网络异常,请重试!");
            }
        });
    }

    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        if (viewId == R.id.btnPay) {
            submitOrder();
        } else if (viewId == R.id.rltWechatPay) {
            payType = SZConst.WXPAY;
            imgCheckWechat.setImageResource(R.mipmap.pay_icon_choose);
            imgCheckAlipay.setImageResource(R.mipmap.pay_icon_not);
        } else if (viewId == R.id.rltAlipay) {
            payType = SZConst.ALIPAY;
            imgCheckAlipay.setImageResource(R.mipmap.pay_icon_choose);
            imgCheckWechat.setImageResource(R.mipmap.pay_icon_not);
        }
    }

    public void requestVipUserInfo(Observable<String> observable) {
        SZRetrofitManager.getInstance().request(observable, "vip", new SZRetrofitResponseListener() {
            @Override
            public void onSuccess(String data) {
                List<VipInfo> vipInfoList = new Gson().fromJson(data, new TypeToken<List<VipInfo>>() {
                }.getType());
                if (SZValidatorUtil.isValidList(vipInfoList)) {
                    vipInfoBought = vipInfoList.get(0);
                } else {
                    vipInfoBought = null;
                }
                setVipUserInfo(vipInfoBought);
            }
            @Override
            public void onNetError(int errorCode, String errorMsg) {
            }
            @Override
            public void onWebServiceError(int errorCode, String errorId, String errorMsg) {
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(vipInfoBought == null) {
            requestVipUserInfo(getVipUserObservable());
        }
    }
    public abstract Observable<String> getVipUserObservable();

    public abstract void setVipUserInfo(VipInfo vipInfo);

    public abstract void onPayDoneSuccess(String payType);

    public abstract String getUserId();
}
